package com.jie.langlangmall.portal.service.impl;

import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.model.UmsMember;
import com.jie.langlangmall.portal.dao.PmsProductCommentDao;
import com.jie.langlangmall.portal.domain.PmsProductComment;
import com.jie.langlangmall.portal.service.ProductCommentService;
import com.jie.langlangmall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/29
 * @Email 3217998214@qq.com
 * @Description 商品评论
 */
@Service
public class ProductCommentServiceImpl implements ProductCommentService {

    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private PmsProductCommentDao productCommentDao;

    @Override
    public List<PmsProductComment> getProductComment(Long productId) {
        return productCommentDao.getByProductId(productId);
    }

    @Override
    public void addProductComment(PmsProductComment productComment) {
        UmsMember currentMember = memberService.getCurrentMember();
        if (currentMember == null) {
            throw new ApiException("未登录");
        }
        productComment.setUserName(currentMember.getNickname());
        productComment.setTime(LocalDateTime.now());
        productComment.setAvatar(currentMember.getIcon());
        productCommentDao.insert(productComment);
    }
}
